import 'package:flutter/material.dart';

class ThemeTestPage extends StatefulWidget {
  @override
  _ThemeTestPageState createState() => _ThemeTestPageState();
}

class _ThemeTestPageState extends State<ThemeTestPage> {
  var _themeColor = Colors.teal; //当前路由主题色
  final List<MaterialColor> _availableColors = [
    Colors.teal,
    Colors.blue,
    Colors.red,
    Colors.green,
    Colors.orange,
    Colors.purple,
    Colors.pink,
    Colors.indigo,
    Colors.amber,
    Colors.cyan,
    Colors.lime,
    Colors.brown,
  ];

  @override
  Widget build(BuildContext context) {
    ThemeData themeData = Theme.of(context);
    return Theme(
      data: ThemeData(
        primarySwatch: _themeColor, //用于导航栏、FloatingActionButton的背景色等
        iconTheme: IconThemeData(color: _themeColor), //用于Icon颜色
      ),
      child: Scaffold(
        appBar: AppBar(title: Text("主题测试")),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            //第一行Icon使用主题中的iconTheme
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Icon(Icons.favorite),
                Icon(Icons.airport_shuttle),
                Text("  颜色跟随主题"),
              ],
            ),
            //为第二行Icon自定义颜色（固定为黑色)
            Theme(
              data: themeData.copyWith(
                iconTheme: themeData.iconTheme.copyWith(color: Colors.black),
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Icon(Icons.favorite),
                  Icon(Icons.airport_shuttle),
                  Text("  颜色固定黑色"),
                ],
              ),
            ),
            // 新增说明文本
            Padding(
              padding: const EdgeInsets.only(top: 16.0),
              child: Text("请选择颜色以切换主题：", style: TextStyle(fontSize: 16)),
            ),
            // 新增颜色选择器
            Padding(
              padding: const EdgeInsets.all(24.0),
              child: Wrap(
                spacing: 8.0,
                runSpacing: 8.0,
                alignment: WrapAlignment.center, // 使内容居中显示
                children:
                    _availableColors.map((color) {
                      return Column(
                        children: [
                          GestureDetector(
                            onTap: () {
                              setState(() {
                                _themeColor = color;
                              });
                            },
                            child: Container(
                              width: 50,
                              height: 50,
                              decoration: BoxDecoration(
                                color: color,
                                shape: BoxShape.circle,
                                border: Border.all(
                                  color:
                                      _themeColor == color
                                          ? Colors.black
                                          : Colors.transparent,
                                  width: 2,
                                ),
                              ),
                            ),
                          ),
                          Text(
                            _getColorName(color),
                            style: TextStyle(fontSize: 12),
                          ),
                        ],
                      );
                    }).toList(),
              ),
            ),
          ],
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            // 切换主题颜色，依次选择_availableColors中的颜色
            setState(() {
              int currentIndex = _availableColors.indexOf(_themeColor);
              int nextIndex = (currentIndex + 1) % _availableColors.length;
              _themeColor = _availableColors[nextIndex];
            });
          },
          child: Icon(Icons.palette),
        ),
      ),
    );
  }

  String _getColorName(MaterialColor color) {
    if (color == Colors.teal) {
      return '青色';
    } else if (color == Colors.blue) {
      return '蓝色';
    } else if (color == Colors.red) {
      return '红色';
    } else if (color == Colors.green) {
      return '绿色';
    } else if (color == Colors.orange) {
      return '橙色';
    } else if (color == Colors.purple) {
      return '紫色';
    } else if (color == Colors.pink) {
      return '粉色';
    } else if (color == Colors.indigo) {
      return '靛蓝';
    } else if (color == Colors.amber) {
      return '琥珀';
    } else if (color == Colors.cyan) {
      return '青色';
    } else if (color == Colors.lime) {
      return '酸橙';
    } else if (color == Colors.brown) {
      return '棕色';
    }
    return '未知';
  }
}
